const connection = require('../app/database')
class FileService {
    async createAvatar(filename, mimetype, size, userId) {
        const statement = `INSERT INTO avatar (filename,mimetype,size,user_id)
        VALUES (?,?,?,?)`
        const [result] = await connection.execute(statement, [filename, mimetype, size, userId])
        return result
    }
    async getAvatarByUserId(userId) {

        const statement = `SELECT * FROM avatar WHERE user_id = ?`
        const [result] = await connection.execute(statement, [userId])
        return result[0]

    }
    // async createFile(filename, mimetype, size, id, momentId) {
    //     console.log(2)
    //     const statement = `INSERT INTO file (filename,mimetype,size,user_id,moment_id)
    //     VALUES (?,?,?,?,?)`
    //     const [result] = await connection.execute(statement, [filename, mimetype, size, id, momentId])

    //     return result
    // }
    async createFile(filename, mimetype, size) {

        const statement = `INSERT INTO file (filename,mimetype,size)
        VALUES (?,?,?)`
        const [result] = await connection.execute(statement, [filename, mimetype, size])

        return result
    }
    async createOtherFile(filename, mimetype, size, originalname) {
        const statement = `INSERT INTO upload (filename,mimetype,size,originalname)
        VALUES (?,?,?,?)`
        const [result] = await connection.execute(statement, [filename, mimetype, size, originalname])

        return result
    }
    async getFileByFilename(filename) {

        const statement = `SELECT * FROM file WHERE filename = ?`
        const [result] = await connection.execute(statement, [filename])

        return result[0]
    }
    async getOtherFileByFilename(filename) {
        const statement = `SELECT * FROM upload WHERE filename = ?`
        const [result] = await connection.execute(statement, [filename])

        return result[0]
    }
    async getOtherFileList() {
        const statement = `SELECT * FROM upload`
        const [result] = await connection.execute(statement)

        return result
    }
}
module.exports = new FileService()